
const int MAX_ITERATIONS = 15;
const int FADE_DELAY = 10;
const int FADE_STEP = 1;
const int BLUE_PIN = 9;
const int GREEN_PIN = 10;
const int RED_PIN = 11;
const int BUZZER_PIN = 5;
const int IR_PIN = A0;
const int IR_ENABLE_PIN = 4;
const int DISTANCE_SENSOR_PIN = A1;
const int distance_ok = 150;


boolean colors[3];
int lastColorIndex;
const int BLUE_POS = 0;
const int GREEN_POS = 1;
const int RED_POS = 2;

int iterations = 0;
boolean isPlaying = true;


void setup() 
{
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(IR_PIN, INPUT);
  pinMode(IR_ENABLE_PIN, OUTPUT);
  pinMode(DISTANCE_SENSOR_PIN, INPUT);
  
  randomSeed(millis());
  resetAllLights();
  
  //Serial.begin(9600);
  
}

void loop() 
{
  if(!isPlaying)
  {
    iterations = 0;
    digitalWrite(IR_ENABLE_PIN, HIGH);
    isPlaying = checkStart();
    digitalWrite(IR_ENABLE_PIN, LOW);
    
    randomSeed(millis());
    //randomColor();
    
    delay(250);
    resetAllLights();    
    
  }
  else
  {
    digitalWrite(IR_ENABLE_PIN, LOW);
    
    if(iterations>=MAX_ITERATIONS)
    {
      isPlaying=false;
      fadeAllOut();
      resetAllLights();
    }
    else
    {
      int index = lastColorIndex;
      while(index==lastColorIndex)
      {
        index = random(3);        
      }
      lastColorIndex = index;
      
      colors[index] = !colors[index];
      int pin;
      if(index==RED_POS) pin=RED_PIN; 
      else if(index==GREEN_POS) pin=GREEN_PIN;
      else if(index==BLUE_POS) pin=BLUE_PIN;
      else pin=RED_PIN;

/*
      if(colors[RED_POS]) Serial.print("RED ");
      if(colors[GREEN_POS]) Serial.print("GREEN ");
      if(colors[BLUE_POS]) Serial.print("BLUE ");
      Serial.println("");
*/  
      fadeLight(pin, colors[index]);

      iterations++;
    }    
  }
}

boolean checkStart()
{
  int distanceValue = analogRead(DISTANCE_SENSOR_PIN);
  int sensor = digitalRead(IR_PIN);
  if(sensor==HIGH && distanceValue<distance_ok)
  {
    //Serial.println("high");
    return false;
  }
  else
  {
    //Serial.println("low");
    return true;  
  }
  
}

void fadeAllOut()
{
    for(int fadeValue = 254 ; fadeValue >= 0; fadeValue -=FADE_STEP) 
    {
      if(colors[RED_POS]) analogWrite(RED_PIN, fadeValue);
      if(colors[GREEN_POS]) analogWrite(GREEN_PIN, fadeValue);
      if(colors[BLUE_POS]) analogWrite(BLUE_PIN, fadeValue);
      delay(FADE_DELAY);
    }  
}

void resetAllLights()
{
  analogWrite(GREEN_PIN, 0);
  analogWrite(BLUE_PIN, 0);
  analogWrite(RED_PIN, 0);
  
  lastColorIndex = GREEN_POS;
  colors[BLUE_POS] = false;
  colors[GREEN_POS] = false;
  colors[RED_POS] = false;
}

void randomColor()
{
  analogWrite(GREEN_PIN, random(255));
  analogWrite(BLUE_PIN, random(255));
  analogWrite(RED_PIN, random(255));
}

void fadeLight(int pin, boolean up)
{
  if(up)
  {
    for(int fadeValue = 0 ; fadeValue <=254; fadeValue +=FADE_STEP)
    {
      analogWrite(pin, fadeValue);
      delay(FADE_DELAY);
    }
  }
  else
  {
    for(int fadeValue = 254 ; fadeValue >= 0; fadeValue -=FADE_STEP) 
    {
      analogWrite(pin, fadeValue);
      delay(FADE_DELAY);
    }
  } 
}


